package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.Resume;
import com.hpjpw.ido.domain.ResumeReceived;
import com.hpjpw.ido.service.IResumeReceivedService;
import com.hpjpw.ido.service.IResumeService;
import com.hpjpw.idoapp.service.IDoResumeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 路瞳
 * @Data
 */

@RestController
@RequestMapping("/ido/app/resume")
public class IDoResumeController extends BaseController {

    @Autowired
    IResumeService idoResumeService;
    @Autowired
    IResumeReceivedService idoResumeReceivedService;
    @Autowired
    IDoResumeService idoAppResumeService;
    @Autowired
    ISysUserService sysUserService;

    private Long statePost = 0L;
    private Long stateReceived = 1L;
    private Long stateRefuse = 2L;

    /**
     * 更新简历内容
     */
    @PostMapping("updateResume")
    @ApiOperation("更新简历内容,参数为- id(简历编号)\n" +
            "- 添加新简历,参数为 - uId(用户id)\n"+
            "- selfPicture(证件照)\n" +
            "- name(姓名)\n" +
            "- phone(联系方式)\n" +
            "- email(邮箱)\n" +
            "- purpose(求职意向)\n" +
            "- selfIntroduction(自我介绍)\n" +
            "- honor(证书/荣誉(仿就业平台))\n" +
            "- experience(工作/项目经历(仿就业平台))\n" +
            "- resumePath(上传简历路径)")
    public AjaxResult updateResume(@RequestBody Resume resume){
        /* 安全校验1，无该发布者*/
        if (StringUtils.isNull(sysUserService.selectUserById(resume.getuId()))){
            return AjaxResult.error("错误！没有此发布者，请检查Uid！");
        }
        /* 安全校验2，无此简历 */
        if (StringUtils.isNull(idoResumeService.selectResumeById(resume.getId()))){
            return AjaxResult.error("错误！无此简历,请新建一个简历");
        }

        Resume newResume = new Resume();
        newResume.setId(resume.getId());
        newResume.setuId(resume.getuId());
        newResume.setName(resume.getName());
        newResume.setSelfPicture(resume.getSelfPicture());
        newResume.setEmail(resume.getEmail());
        newResume.setPhone(resume.getPhone());
        newResume.setSelfIntroduction(resume.getSelfIntroduction());
        newResume.setPurpose(resume.getPurpose());
        newResume.setHonor(resume.getHonor());
        newResume.setExperience(resume.getExperience());
        newResume.setResumePath(resume.getResumePath());


        idoResumeService.updateResume(newResume);
        return AjaxResult.success();
    }

    /**
    * 添加新简历
    */
    @PostMapping("addResume")
    @ApiOperation("添加新简历,参数为 - uId(用户id)\n" +
            "- selfPicture(证件照)\n" +
            "- name(姓名)\n" +
            "- phone(联系方式)\n" +
            "- email(邮箱)\n" +
            "- purpose(求职意向)\n" +
            "- selfIntroduction(自我介绍)\n" +
            "- honor(证书/荣誉(仿就业平台))\n" +
            "- experience(工作/项目经历(仿就业平台))\n" +
            "- resumePath(上传简历路径)")
    public AjaxResult addResume(@RequestBody Resume resume){
        /* 安全校验1，无该发布者*/
        if (StringUtils.isNull(sysUserService.selectUserById(resume.getuId()))){
            return AjaxResult.error("错误！没有此发布者，请检查Uid！");
        }

        Resume resumeadd = new Resume();
        resumeadd.setuId(resume.getuId());
        resumeadd.setName(resume.getName());
        resumeadd.setSelfPicture(resume.getSelfPicture());
        resumeadd.setEmail(resume.getEmail());
        resumeadd.setPhone(resume.getPhone());
        resumeadd.setSelfIntroduction(resume.getSelfIntroduction());
        resumeadd.setPurpose(resume.getPurpose());
        resumeadd.setHonor(resume.getHonor());
        resumeadd.setExperience(resume.getExperience());
        resumeadd.setResumePath(resume.getResumePath());

        idoResumeService.insertResume(resumeadd);
        return AjaxResult.success();
    }

    /**
     * 查询当前用户简历列表
     */
    @ApiOperation("查询用户的简历列表,接收参数 uid(用户id)")
    @GetMapping("/listResume")
    public TableDataInfo query(@RequestBody long u_id){
        List<Map<String, String>> mapList = idoAppResumeService.personnalResumeList(u_id);
        return getDataTable(mapList);
    }


    /**
     * 投递简历
     */
    @PostMapping("postResume")
    @ApiOperation("投递简历,接收参数 - applyId(投递者id)" +
            "- enterpriseId(企业id)" +
            "- resumeId(简历id)")
    public AjaxResult postResume(@RequestBody ResumeReceived received){
        /* 安全校验1，无该投递者*/
        if (StringUtils.isNull(sysUserService.selectUserById(received.getApplyId()))){
            return AjaxResult.error("错误！没有此投递者，请检查用户id！");
        }
        /* 安全校验2，无此简历 */
        if (StringUtils.isNull(idoResumeService.selectResumeById(received.getResumeId()))){
            return AjaxResult.error("错误！无此简历,请新建一个简历");
        }
        /* 安全校验3，没有该企业 -- */
//        if (StringUtils.isNull(){
//            return AjaxResult.error("错误！没有该企业,请检查企业id");
//        }

        ResumeReceived resumeReceived = new ResumeReceived();
        resumeReceived.setApplyId(received.getApplyId());
        resumeReceived.setEnterpriseId(received.getEnterpriseId());
        resumeReceived.setTime(new Date());
        resumeReceived.setResumeId(received.getResumeId());
        resumeReceived.setState(statePost);

        idoResumeReceivedService.insertResumeReceived(resumeReceived);
        return AjaxResult.success();
    }

    /**
     * 查询某企业的所有简历列表
     */
    @ApiOperation("查询该企业的所有简历列表,接收参数 enterpriceId(企业id)" +
            "可带分页参数：pageNum=1&pageSize=10")
    @GetMapping("/ResumeList/{enterpriceId}")
    public TableDataInfo queryAll(@PathVariable("enterpriceId") long enterpriceId){
        /* 应有安全校验1，没有该企业 -- */
//        if (StringUtils.isNull(){
//            return AjaxResult.error("错误！没有该企业,请检查企业id");
//        }
        startPage();
        List<Map<String, String>> mapList = idoAppResumeService.allResumes(enterpriceId);
        return getDataTable(mapList);
    }

    /**
     * 更改某条投递简历记录中简历状态
     */
    @PostMapping("setResumeReceivedSate")
    @ApiOperation("更新简历状态,参数为 - operate_id(操作者id)" +
            "resumeReId(简历记录id)" +
            "State(目标状态 --  1,已采纳  2,已拒绝)")
    public AjaxResult updateResumeState(@Param("operate_id") Long operate_id,@Param("resumeReceivedId")Long resumeReceivedId ,@Param("State")Long state){

        /* 安全校验1，无此简历记录 */
        if (StringUtils.isNull(idoResumeReceivedService.selectResumeReceivedById(resumeReceivedId))){
            return AjaxResult.error("错误！无此简历记录");
        }
        /* 安全校验2，无该投递者*/
        if (StringUtils.isNull(sysUserService.selectUserById(operate_id))){
            return AjaxResult.error("错误！没有此操作者，请检查用户id！");
        }

        idoResumeReceivedService.updateResumeReceivedState(resumeReceivedId,state);
        return AjaxResult.success();
    }


}
